// pages/SCM/store/goods/goods.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: [],
    categories: [],
    tabNum: 0,
    pageNum: 1,
    pageSize: 15,
    goodsList: [],
    goodsStatIds: [],
    hideFlag: true,
    haveLogisticsList: [{
      id: 0,
      name: '无物流'
    }, {
      id: 1,
      name: '有物流'
    }],
    haveLogisticsId: 0,
    haveLogisticsName: '无物流',
    applyStatus: [{
      id: 0,
      name: '待确认收货'
    }, {
      id: 1,
      name: '已收货'
    }],
    uindex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    let that = this
    that.toast = that.selectComponent("#toast")
    this.personalModal = this.selectComponent("#personalModal")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.waitReceiveList();
    let that = this
    //


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },






  toggleTab: function(e) {
    let id = e.currentTarget.dataset.id;
    if (id != this.data.tabNum) {
      this.setData({
        tabNum: id,
        pageNum: 1
      });
      this.waitReceiveList();

    }

  },
  getMore: function() {
    this.data.pageNum += 1;
    this.waitReceiveList();
  },
  getFirst: function() {
    this.data.pageNum == 1;
    this.waitReceiveList();
  },
  waitReceiveList: function() {
    let that = this;
    let params = {};
    // if(!that.data.getMore){
    //      that.data.pageNum=1;   
    // }
    // that.data.getMore=false;
    let tabNum = that.data.tabNum;
    params.stat = tabNum
    params.pageNum = that.data.pageNum;
    params.pageSize = that.data.pageSize;
    lbbService.waitReceiveList(params, function(res) {
      let waitReceiveList = res.data;
      waitReceiveList.forEach(e => {
        if (e.flowType=='apply'){
          //备货明细
          let goodsApplyUps = e.goodsApplyUps;
          let packs = [];
          goodsApplyUps.forEach((item, i) => {
            let pack = {};
            pack.packName = '包裹' + (i + 1);
            pack.packStat = item.stat;
            pack.id = item.id;
            let upDetailsList = item.upDetailsList;
            let upStrs = [];
            upDetailsList.forEach(ele => {
              upStrs.push(ele.goodsName + ' X' + ele.deliverNum);
            })
            let str = upStrs.join('、');
            pack.packGoodsStrs = str;
            packs.push(pack);
          })
          e.packs = packs;
        }
        
      })
      if (that.data.pageNum > 1) {
        waitReceiveList = that.data.waitReceiveList.concat(res.data);
      }
      that.setData({
        waitReceiveList: waitReceiveList
      });
    });

  },
  goodsSendConfirm: function() {
    let that = this
    lbbService.goodsSendConfirm({
      id: that.data.id
    }, function(res) {
      wx.showToast({
        title: '提交成功',
      })
      let goodsSend = that.data.goodsSend;
      goodsSend.sendStatus = 1;
      that.setData({
        goodsSend: goodsSend
      });
    })
  },
  toSendItem: function(e) {
    let item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '../goodsSendItem/goodsSendItem?id=' + item.id,
    })
  },
  toApplyItem: function(e) {
    let item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '../goodsApplyItem/goodsApplyItem?id=' + item.id+'&packId='+item.packId,
    })
  },
  copyPackInfo: function (e) {
    let pack = e.target.dataset.pack;
    let str = '';
    str += pack.packName + '：' + (pack.packStat == 1 ? '已收货' : '待收货') + '\n';
    str += '发货物品：' + pack.packGoodsStrs + '\n';
    wx.setClipboardData({
      data: str,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功',
            })
          }
        })
      }
    })

  },
  
})